//
//  LDBaseTagsView.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/4/27.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit
import pop

class LDBaseTagsView: UIView {
    var tagViewNotiName = "tapTagLabsViewNotiName"
    var anchorPoint:CGPoint?
    var SameLabTag = 888
    var sendColsure:((index:Int) -> Void)?
    var sendParamsColsure: ((params:NSMutableArray)->())?//Dictionary<String,String>
    var totalLabHeight:CGFloat?
    
    var contentScrollView:UIScrollView?
    //
    var selLabArrParams = NSMutableArray()
    //判断 根控制器
    internal var control = String()//[String:String]()
    //判断选中
    var isSelectStatus:Bool = false {
        didSet{
            if isSelectStatus == true {
                
            }
        }
    }
    var titleParams = [String]() {
        didSet{
            setDefaultContent()
        }
    }
    //保存tag对应的id号
    var title_id:Array<String>? // = [String]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = LDGolbalColor.White
        
        contentScrollView = UIScrollView.init(frame: bounds)
    }
    
    func setDefaultContent() {
        let count:Int = titleParams.count
        
        var labX:CGFloat = Margin_V*2
        var labY:CGFloat = Margin_V*2
        var totalW:CGFloat = Margin_V*4
        
        let labH:CGFloat = 35
        
        for i in 0..<count {
            
            let lab = UILabel.setDetailsLab(titleParams[i], fontSize: 13, titleColor: LDFontColor.DeepGray, Alignment: .Center)
            
            if anchorPoint != nil {
                lab.frame.origin.x = anchorPoint!.x
                lab.frame.origin.y = anchorPoint!.y
            }
            addSubview(lab)
            
            lab.backgroundColor = LDGolbalColor.White
            lab.layer.cornerRadius = 3
            lab.layer.borderWidth = 1
            lab.layer.borderColor = LDIconColor.LightGray.CGColor
            lab.userInteractionEnabled = true
            lab.enabled = false
            
            lab.bk_whenTapped({
                [weak self] in
                self?.sendNoti(i+1,tagTitle: (self?.titleParams[i])!,control: (self?.control)!,lab:lab)
                })
            
            let rect = NSString.init(string: titleParams[i]).boundingRectWithSize(CGSizeMake(300, 13), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font13], context: nil)
            
            let labW = rect.width + LDMargin_V*2
            totalW += labW + LDMargin_H*2
            
            if totalW > (self.width) {
                totalW = Margin_H*4 + labW
                labX = Margin_V*2
                labY = labY + labH + LDMargin_V*2
            }
            totalLabHeight = labY + labH
            UIView.animateWithDuration(0.4, delay: 0,
                                       options: UIViewAnimationOptions.TransitionFlipFromLeft,
                                       animations: { [weak self] in
                
                lab.frame = CGRectMake(labX, labY, labW, labH)
                self?.layoutIfNeeded()
        
                }, completion: nil)
            
            labX = labX + labW + LDMargin_H*2
        }
//        
//        if (totalLabHeight! + LDFrame.NavH) > LDFrame.ScreenHeight {
//            contentScrollView?.contentSize = CGSizeMake(LDFrame.ScreenWidth, totalLabHeight! + LDFrame.NavH)
//        }
    }
    //通知 n控制器
    func sendNoti(index:Int,tagTitle:String,control:String,lab:UILabel) {
        
        if sendColsure != nil {
            sendColsure!(index: index)
        }
        
        if selLabArrParams.count >= 3 && lab.enabled == false {
                        
            showLDAlertView("便签不能大于3个!")
            
            lab.textColor = LDFontColor.DeepGray
            lab.backgroundColor = LDGolbalColor.White
            return
        }
        
        if isSelectStatus == true {
            
            lab.enabled = !lab.enabled
            let dict = ["\(index)"]//["industriesIDs":"\(index)"]
            
            if lab.enabled {
                lab.layer.masksToBounds = true
                lab.backgroundColor = LDGolbalColor.GreenAssist
                
                
                if selLabArrParams.containsObject(dict) == false {
                    selLabArrParams.addObject(dict)
                }
            } else {
                
                lab.backgroundColor = LDGolbalColor.White
                lab.textColor = LDFontColor.DeepGray
                
                if selLabArrParams.containsObject(dict) == true {
                    selLabArrParams.removeObject(dict)
                }
            }
            debugPrint(selLabArrParams.count,"selLabArrParams.count")
        }
        
        if sendParamsColsure != nil {
            sendParamsColsure!(params: selLabArrParams)
        }
        
        if title_id != nil {
            
            let sel_id = title_id![index-1]
            NSNotificationCenter.defaultCenter().postNotificationName(tagViewNotiName, object: self, userInfo: ["index":sel_id,"tag":tagTitle,"control":control])
        } else {
            
            NSNotificationCenter.defaultCenter().postNotificationName(tagViewNotiName, object: self, userInfo: ["index":"\(index)","tag":tagTitle,"control":control])
        }
    }
    
    func tapLabsColsure(colsure: ((index:Int)->())?) {
        sendColsure = colsure
    }
    func sendManyLabsParams(colsure:(params:NSMutableArray)->()) {
        sendParamsColsure = colsure
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
